import bold.deposit.AsyncBean;
import bold.deposit.event.MyPubisher;
import bold.storage.component.BaseDao;
import bold.storage.core.DB;
import bold.storage.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.web.context.WebApplicationContext;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
 * @author Khai Loon
 * @version 2017/8/17
 */

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath*:/spring-config.xml", "classpath*:/spring-mvc.xml"})//加载配置文件
public class Core extends BaseDao {

    @Autowired
    private WebApplicationContext webContext;

    private MockMvc mockMvc;
    private MockHttpServletRequestBuilder mockBuilder;
    private MockHttpServletResponse mockResponse;

    private String token = "Bearer NrmVNunpaFleC3BcKdd2X22zEhoPmzO8sdzJ1BI3khFPYz6EVbWkFfU4grBioEofB/GEfu9S0mVhU2rBxU34PYVVAR7srwJqC5jx/z4yL6IECIOcLzQpAtv5bhkN8Du/lY0NIYqw2EOj5v/3t1t6z80MZOT1gHXe/qYe6zRRHAGu8kA28mrWFsUSBN0+jdhnpB/+o7gBM/vB2ZsixffjgN0Z/HOD4auwFsTGTy+YrlmXfC2BAzQOw9oBNUVMSLEP9Gooo0xQ0HgpuureeYiZtNaTwzIwcptvwcWvVMFBcwcA5/qyykQrKFE0dhiQG0uAkMFL+o27hiczqUN86EjMILookqkGwYo6U9mxYp7mGiswAF1L4YsAGJtTqGJiWnfK2e/40PUwyT86NeFilnlZoAr6usnkCi8DUeTM/ksBboJp7GsWaWd4H/u5EJtRrSwTfG5BFSe/ZsdBGEZ2c5ImjmMepRk+7L4qkK+l7Oy/csA=";
    private static String baseUrl = "http://127.0.0.1:89/";


    private Map<String, String> dataMap = new HashMap<>();

    public void buildPost(String url, Map<String, String> params) throws Exception {
        mockMvc = webAppContextSetup(webContext).build();

        mockBuilder = post(baseUrl + url);
        mockBuilder.header("Authorization", token);

        if (!params.isEmpty()) {
            params.keySet().stream()
                    .filter(key -> !Objects.equals(params.get(key), ""))
                    .forEach(key -> mockBuilder.param(key, params.get(key)));
        }

        ResultActions resAc = mockMvc.perform(mockBuilder);
        resAc.andExpect(status().isOk());
        mockResponse = resAc.andReturn().getResponse();
        JSONObject jsonObject = JSON.parseObject(mockResponse.getContentAsString());
        String code = jsonObject.get("code").toString();
        Assert.assertEquals("状态==200", code, "200");
        System.out.println(jsonObject.get("data"));
    }


    @Test
    public void test1() {
        MyPubisher myPubisher = webContext.getBean(MyPubisher.class);
        myPubisher.publishEvent();
    }


    @Test
    public void Async() throws InterruptedException, ExecutionException, TimeoutException {
        Future<String> result = webContext.getBean(AsyncBean.class).async();

        System.out.println("主线程继续");
        Thread.sleep(3000L);

        //此处用法很多
        System.out.println(result.get(2, TimeUnit.SECONDS));
    }


    @Test
    public void test2() throws SQLException {
        String sql = XmlUtil.get("v_table");
        String s = DB.query(sql).toString();
        System.out.println(s);
    }

    public static void main(String[] args) {
        System.out.println(JSON.toJSONString("3123    , 123123  \n, 3123\\123123".split("\\s*,\\s*")));
        System.out.println(JSON.toJSONString(" af d ".trim()));
        System.out.println("AAa".equalsIgnoreCase("aaa"));

    }


}

